package com.ruoyi.decision.service.impl;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.decision.domain.Menu;
import com.ruoyi.decision.domain.MenuModel;
import com.ruoyi.decision.domain.Model;
import com.ruoyi.decision.domain.Scene;
import com.ruoyi.decision.mapper.SPlmRtsModelMapper;
import com.ruoyi.decision.service.IModelService;
import com.ruoyi.system.service.ISysDictDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.UUID;

/**
 * 模型Service业务层处理
 * 
 * @author zbw
 * @date 2022-07-20
 */
@Service
public class ModelServiceImpl implements IModelService
{
    @Autowired
    private SPlmRtsModelMapper sPlmRtsModelMapper;

    @Autowired
    private ISysDictDataService selectDictDataList;

    /**
     * 查询模型
     * 
     * @param modelId 模型主键
     * @return 模型
     */
    @Override
    public List<MenuModel> selectSPlmRtsModelByModelId(String modelId)
    {
        Model model = sPlmRtsModelMapper.selectSPlmRtsModelByModelId(modelId);
        String modeJson = model.getModeJson();

        List<MenuModel> list = JSON.parseArray(modeJson, MenuModel.class);


        return list;
    }

    /**
     * 查询模型列表
     * 
     * @param model 模型
     * @return 模型
     */
    @Override
    public List<Model> selectSPlmRtsModelList(Model model)
    {
        return sPlmRtsModelMapper.selectSPlmRtsModelList(model);
    }

    /**
     * 新增模型
     * 
     * @param model 模型
     * @return 结果
     */
    @Override
    public int insertSPlmRtsModel(Model model)
    {
        String modelTypeId = UUID.randomUUID().toString().toLowerCase(Locale.ROOT).replace("-","");
        model.setModelId(modelTypeId);
        return sPlmRtsModelMapper.insertSPlmRtsModel(model);
    }

    /**
     * 修改模型
     * 
     * @param model 模型
     * @return 结果
     */
    @Override
    public int updateSPlmRtsModel(Model model)
    {
        return sPlmRtsModelMapper.updateSPlmRtsModel(model);
    }

    /**
     * 批量删除模型
     * 
     * @param modelIds 需要删除的模型主键
     * @return 结果
     */
    @Override
    public int deleteSPlmRtsModelByModelIds(String[] modelIds)
    {
        return sPlmRtsModelMapper.deleteSPlmRtsModelByModelIds(modelIds);
    }

    /**
     * 删除模型信息
     * 
     * @param modelId 模型主键
     * @return 结果
     */
    @Override
    public int deleteSPlmRtsModelByModelId(String modelId)
    {
        return sPlmRtsModelMapper.deleteSPlmRtsModelByModelId(modelId);
    }

    @Override
    public List<Menu> treemenu(Model model) {
        String a="rts_scene_type";
        SysDictData sysDictData1 = new SysDictData();
        sysDictData1.setDictType(a);
        List<SysDictData> sysDictData = selectDictDataList.selectDictDataList(sysDictData1);
        List<Menu> data = new ArrayList<>();
        List<Model> models = sPlmRtsModelMapper.selectSPlmRtsModelList(null);
        for (SysDictData b : sysDictData) {
            Menu vo = new Menu();
            vo.setId(b.getDictCode().toString());
            vo.setName(b.getDictLabel());
            vo.setpId(b.getDictValue());
            Menu menu = new Menu();
            for (Model models1 : models) {
                if (vo.getpId().equals(models1.getModelTypeId())){
                    List<Menu> s = new ArrayList<>();
                    menu.setName(models1.getModelName());
                    menu.setId(models1.getModelId());
                    menu.setpId(models1.getModelTypeId());
                    s.add(menu);
                    vo.setChildren(s);
                }
            }

            data.add(vo);
        }

        return data;
    }
}
